package TP;

import fr.inria.edelweiss.kgraph.core.Graph;
import fr.inria.edelweiss.kgraph.rule.RuleEngine;
import fr.inria.edelweiss.kgtool.load.Load;
import fr.inria.edelweiss.kgtool.load.RuleLoad;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;

public class Server {

    public static Graph graph;
    
    public static Graph getGraph(ServletContext context) {

        //Graph graph = (Graph) context.getAttribute("graph"); // Graph global
        if (null == graph) {
                        Logger.getLogger(IndexServlet.class.getName()).log(Level.SEVERE, "Création du graphe");
            graph = Graph.create(true);

            // Chargement des fichiers rdf
            Load ld = Load.create(graph);
            String[] files = {
                "artists-fr.rdf",
                "artists-hometown.rdf",
                "bands-fr.rdf",
                "bands-hometown.rdf",
                "bands-members.rdf",
                "genres.rdf",
                "hometown-location.rdf",
                "featuring.rdf",
                
                "geoband.owl"
            };
            
            for(String file : files)
                ld.load(context.getRealPath("/WEB-INF/resources/"+file));
            
            // Mise en place des règles
            RuleEngine engine = RuleEngine.create(graph);
            RuleLoad rld = RuleLoad.create(engine);
            rld.load(context.getRealPath("/WEB-INF/resources/rules.rul"));
            engine.process();
            
            
            
            // Sauvegarde du graph courant dans le contexte
            //context.setAttribute("graph", graph);
        }
        return graph;
    }
}
